﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace AutoStar
{
    class CalculateMD5Hash
    {
        public string GetMD5Hash(string input, int iter)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }

            string output = sb.ToString();
            System.Console.WriteLine(iter + " > " + output);
            if (iter>0)
            {
                iter--;
                output = GetMD5Hash(output, iter);
            }
            return output;
        }
    }
}
